/**
 * 自定义常用功能mixins
 */
import { mapActions, mapState } from 'vuex'
import api_home from '@/api/api_home'
import * as Constant from '@/utils/constant'
import {COMMON} from '@/utils/commonUtil'
import _remove from 'lodash/remove'
let FunctionConfig = {
  data () {
    return {
      // 自定义常用功能
      functionDislogVisible: false,
      functionLoading: false,
      functionList: []
    }
  },
  created () {
    this.functionLoadUseList({_this: this, payload: {}})
  },
  methods: {
    ...mapActions('home', [
      'functionLoadUseList',
      'saveUsualFunc'
    ]),
    openFunctionSetting () { // 打开常用功能配置
      this.functionDislogVisible = true
      this.functionLoading = true
      api_home.functionLoadAll({}, response => {
        if (response.code === Constant.RT_SUCCESS) {
          this.setFunctionList(this.functionGirdData, response.data)
          this.functionLoading = false
        }
      })
    },
    closeFunctionConfig () { // 关闭常用功能配置
      this.functionDislogVisible = false
    },
    closeFunctionSubmiting () { // 提交loading
      this.$refs.FunctionConfig.closeSubmiting()
    },
    submitFormFunction (data) { // 提交
      this.saveUsualFunc({_this: this, data})
    },
    setFunctionList (versionFuncList, arrFuncTreeData) { // 常用功能功能赋值，以及初始化isCheck
      // 初始化分类
      let systemTypes = JSON.parse(JSON.stringify(this.systemTypes))
      _remove(systemTypes, (item, index) => {
        return index === 0
      })
      let functionList = systemTypes.map((item) => {
        return {
          systemName: item.dictText,
          systemType: item.dictKey,
          children: []
        }
      })
      // let functionList = [
      //   {
      //     systemName: Constant.SYSTEM_APP_TYPE_1.label,
      //     children: []
      //   },
      //   {
      //     systemName: Constant.SYSTEM_APP_TYPE_2.label,
      //     children: []
      //   },
      //   {
      //     systemName: Constant.SYSTEM_APP_TYPE_3.label,
      //     children: []
      //   }
      // ]
      arrFuncTreeData.forEach((itemLevel1, index) => {
        for (let i = 0; i < versionFuncList.length; i++) {
          if (versionFuncList[i].systemInfoId === itemLevel1.id) {
            itemLevel1.isCheck = true
            break
          } else {
            if (!itemLevel1.isCheck) {
              itemLevel1.isCheck = false
            }
          }
        }
        for (let i = 0; i < functionList.length; i++) {
          if (functionList[i].systemType === itemLevel1.systemType) {
            functionList[i].children.push(itemLevel1)
            break
          }
        }
        // switch (itemLevel1.systemType) {
        //   case Constant.SYSTEM_APP_TYPE_1.value:
        //     functionList[0].children.push(itemLevel1)
        //     break
        //   case Constant.SYSTEM_APP_TYPE_2.value:
        //     functionList[1].children.push(itemLevel1)
        //     break
        //   case Constant.SYSTEM_APP_TYPE_3.value:
        //     functionList[2].children.push(itemLevel1)
        //     break
        //   default:
        //     break
        // }
      })
      this.functionList = functionList
    }
  },
  computed: {
    ...mapState({
      functionGirdData: state => state.home.functionGirdData,
      functionGirdPage: state => state.home.functionGirdPage
    })
  }
}
export default FunctionConfig
